package app.dao;

import app.entity.Role;
import org.jdbi.v3.sqlobject.config.RegisterBeanMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.customizer.BindBean;
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;

import java.util.List;
import java.util.Optional;

@RegisterBeanMapper(Role.class) // 注册 Role 类的映射
public interface RoleDao {

    // 插入一条记录
    @SqlUpdate("INSERT INTO role (name, code, remark) VALUES (:name, :code, :remark)")
    @GetGeneratedKeys // 获取自增主键
    Long insert(@BindBean Role role);

    // 根据 ID 查询
    @SqlQuery("SELECT * FROM role WHERE id = :id")
    Optional<Role> findById(@Bind("id") Long id);

    // 查询所有记录
    @SqlQuery("SELECT * FROM role")
    List<Role> findAll();

    // 根据 ID 更新记录
    @SqlUpdate("UPDATE role SET name = :name, code = :code, remark = :remark WHERE id = :id")
    void update(@BindBean Role role);

    // 根据 ID 删除记录
    @SqlUpdate("DELETE FROM role WHERE id = :id")
    void deleteById(@Bind("id") Long id);

    // 根据 code 查询
    @SqlQuery("SELECT * FROM role WHERE code = :code")
    Optional<Role> findByCode(@Bind("code") String code);
}
